package dk.isten.andro.julesangbog;


import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import util.Item;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Sang_List_Act extends Activity implements OnItemClickListener, OnClickListener{
	
	static ArrayList<util.Item> sange = new ArrayList<util.Item>();
	String sponsor;
	private AdView adView;


    @SuppressWarnings("unchecked")
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_act);
        
        adView = new AdView(this,AdSize.BANNER,"a150c885400968e");
        LinearLayout ll= (LinearLayout) findViewById(R.id.adView);
        ll.addView(adView);
        
        AdRequest ar = new AdRequest();
        adView.loadAd(ar);
        
        if(sange.size() == 0){
        	Parse();
        }
        
        ListView listView2 = (ListView) findViewById(R.id.ListView02);
		listView2.setOnItemClickListener(this);
		
		//s�t indholdet af listview.
		listView2.setAdapter(new ArrayAdapter(this, R.layout.listeelement_trup, R.id.listeelem_overskrift, sange) {
			
			@Override
			public View getView(int position, View cachedView, ViewGroup parent) {
				View view = super.getView(position, cachedView, parent);
				
//				if(!spillere.get(position).getSponsor().equals("0")){
//					sponsor = "Sponseret af: " + spillere.get(position).getSponsor();
//					Log.d("sponsor felt", spillere.get(position).getSponsor() );
//				}else{
//					sponsor = spillere.get(position).getPosition(); 
//				}
//				
				TextView listeelem_overskrift = (TextView) view.findViewById(R.id.listeelem_overskrift); 	//s�t layout for TextView
				listeelem_overskrift.setText(sange.get(position).getTitel()); 			//SetText fra ArrayList
				TextView listeelem_beskrivelse = (TextView) view.findViewById(R.id.listeelem_beskrivelse);
				listeelem_beskrivelse.setText(sange.get(position).getForfatter()); 	
				
				return view; 
    }
		});
		}
		
			
			@Override
		    public boolean onCreateOptionsMenu(Menu menu) {
		        getMenuInflater().inflate(R.menu.activity_front_act, menu); 
		        return true;
		    }

			  // Called every time user clicks on a menu item
			  @Override
			  public boolean onOptionsItemSelected(MenuItem item)
			  { 
				  switch (item.getItemId()) 
				  {
				  		case R.id.support:
				  			startActivity(new Intent(this, SupportActivity.class));
				  			break;
				  }  
				  
				  return true;
			  }

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}

			@Override 
			//start en ny activity med en enkelt spiller
			public void onItemClick(AdapterView<?> l, View v, int position, long id) {

				Intent i = new Intent(this, Sang_Act.class);
//				send information om billedIdentifier med intent
				i.putExtra("position", position);
				startActivity(i);
			}
		
			public void Parse() {

				try {
					System.out.println("før inputstream");
					InputStream is = getResources().openRawResource(R.raw.sange);
					
				
					//Standard Java bruger xpp3-1.1.4.jar fra http://www.extreme.indiana.edu/xgws/xsoap/xpp/
					XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
					XmlPullParser xpp=factory.newPullParser();
					System.out.println("efter inputstream+2");
					 
//	XmlPullParser xpp = android.util.Xml.newPullParser();  // Android-kald til at oprette parser
					//xpp.setInput(is, "UTF-8"); // evt "ISO-8859-1"
					xpp.setInput(is, "UTF-8");

					//Bank bank = null; // Husker den aktuelle bank
					Item item = null; // Husker den aktuelle kunde

					int eventType = xpp.getEventType();
							
					while (eventType != XmlPullParser.END_DOCUMENT)
					{
						if (eventType == XmlPullParser.START_TAG)
						{
							item = new Item();
							String s;
							String tag = xpp.getName();
							System.out.println("før parse");
							
							try {
								if ("item".equals(tag))
								//if ("title".equals(tag))
								{
									//skip item
									xpp.nextTag();
									
									//sangtitel
									s = xpp.nextText();
									item.titel = s;
									
									//forfatter
									xpp.nextTag();
									s = xpp.nextText();
									item.forfatter = s;
									
									//�rstal
									xpp.nextTag();
									s = xpp.nextText();
									item.aar = s;

									//sangtekst
									xpp.nextTag();
									s = xpp.nextText();
									item.tekst = s;

														
									//add to arraylist
									System.out.println("efter parse" + item.toString());
									sange.add(item);
								}
							} catch (Exception e) {
								e.printStackTrace();
								System.out.println("An Error occured. The program crashed: " + e.toString());
								//contact app admin
								//show error messages.
							}
						}
						eventType = xpp.next();
					}
				} catch (NotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (XmlPullParserException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			


//				try {
//			
//					InputStream is = getResources().openRawResource(R.raw.sange);
//					byte b[] = new byte[is.available()]; // kun små filer
//					is.read(b);
//					String str = new String(b, "ISO-8859-1"); //UTF-8 virker ikke
//
//					
//					for (String linje : str.split("\n")) {
//						util.Item spiller = new util.Item();
//						
//						linje = linje.trim();
//						if (linje.length() == 0 || linje.startsWith("#")) continue;
//						String[] felter = linje.split(",");
//						spiller.image= felter[0];
//						spiller.navn = felter[1];
//						spiller.sponsor = felter[2];
//						spiller.Troejenummer = felter[3];
//						spiller.position = felter[4];
//						spillere.add(spiller);
//					}
//
//				} catch (Exception ex) {
//					ex.printStackTrace();
//					System.out.println("FEJL:" + ex.toString());
//				}

			}
		
